// import
import Vue from 'vue'
import Vuex from 'vuex'
import reshandler from 'services/response-handler'
// 查询车辆信息列表
import queryCarInfoList from "./QueryCarInfoList"
// 查询维修信息
import queryRepairAppointment from "services/appointment/QueryRepairAppointment";
// 查询保养信息
import queryMaintainAppointment from "services/appointment/QueryMaintainAppointment";

const appointStore = new Vuex.Store({
  state:{
    services: null, // 服务项目
    unReceivedCoupons: [], // 未领取的优惠券
    couponCodes: [], // 可使用的优惠券
    couponIndex: 0
  },
  getters:{
    getServices:function (state) {
      if(!state.services){
        state.services = null
      }
      return state.services;
    },
    getUnReceivedCoupons:function (state) {
      if(!state.unReceivedCoupons){
        state.unReceivedCoupons = []
      }
      return state.unReceivedCoupons;
    },
    getCouponCodes:function (state) {
      if(!state.couponCodes){
        state.couponCodes = []
      }
      return state.couponCodes;
    },
    getCouponIndex:function (state) {
      return state.couponIndex;
    }
  },
  mutations:{
    setServices(state,services) {
      //console.log(state)//state对象
      state.services = services;
    },
    setUnReceivedCoupons(state,unReceivedCoupons) {
      //console.log(state)//state对象
      state.unReceivedCoupons = unReceivedCoupons;
    },
    setCouponCodes(state,couponCodes) {
      //console.log(state)//state对象
      state.couponCodes = couponCodes;
    },
    setCouponIndex(state,couponIndex) {
      //console.log(state)//state对象
      state.couponIndex = couponIndex;
    }
  }
})

export default appointStore

// export
// 查询车辆信息列表
export var QueryCarInfoList = queryCarInfoList
// 查询维修信息
export var QueryRepairAppointment = queryRepairAppointment
// 查询保养信息
export var QueryMaintainAppointment = queryMaintainAppointment

/*
 * 查询取消预约项目列表 response
 */
function makeListCancelledAppointmentItemData(response) {
  return Object.assign({}, response, {
    items: response.items || []
  })
}

// 查询取消预约项目列表
export function ListCancelledAppointmentItem(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "ListAppointmentItemByCancel",
  })
  
  // invoke
  this.$.get(params)
  .then(reshandler.success(success, fail, makeListCancelledAppointmentItemData), reshandler.error(fail))
}

/*
 * 填写订单数据 response
 */
function makeFillInOrderData(response) {
  return Object.assign({}, response, {})
}

// 填写订单数据
export function FillInOrder(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "FillInOrder",
  })
  
  // invoke
  this.$.get(params)
  .then(reshandler.success(success, fail, makeFillInOrderData), reshandler.error(fail))
}

/*
 * 创建订单数据 response
 */
function makeCreateOrderData(response) {
  return Object.assign({}, response, {})
}

// 创建订单数据
export function CreateOrder(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "CreateOrder",
  })
  
  // invoke
  this.$.get(params)
  .then(reshandler.success(success, fail, makeCreateOrderData), reshandler.error(fail))
}

/*
 * 计算订单价格 response
 */
function makeCalculateOrderData(response) {
  return Object.assign({}, response, {})
}

// 计算订单价格
export function CalculateOrder(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "CalculateOrder",
  })
  
  // invoke
  this.$.get(params)
  .then(reshandler.success(success, fail, makeCalculateOrderData), reshandler.error(fail))
}

// 预估服务费用
export function getEstimatePrice(services, couponCodes) {
  // 优惠劵等消息需要计算进去
  if (services && services.cartItems && services.cartItems.length > 0) {
    let price = 0;
    let couponAmount = 0;
    services.cartItems.forEach(item => {
      console.log(couponCodes);
      price += Number(item.product.price) * Number(item.quantity);
    });
    let couponIndex = appointStore.getters.getCouponIndex
    if (couponIndex >= 0 && couponCodes && couponCodes.length > 0) {
      appointStore.commit("setCouponIndex", couponIndex)
      couponAmount = Number(couponCodes[couponIndex].coupon.amount);
    }
    price = price - couponAmount;
    return `¥${price.toFixed(2)}`;
  } else {
    return "¥0.00";
  }
}

